package ru.tennisworld.user.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Component("encryptionUtils")
public class EncryptionUtils {

    private static String publicKey;
    @Value("${rsa-encryption.public-key}")
    public void setPublicKey(String publicKey) {
        EncryptionUtils.publicKey = publicKey;
    }

    private static String privateKey;
    @Value("${rsa-encryption.private-key}")
    public void setPrivateKey(String privateKey) {
        EncryptionUtils.privateKey = privateKey;
    }

    public static String encryptStringValue(String data) throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
        return RsaUtil.encrypt(data, publicKey);
    }

    public static String decryptStringValue(String data) throws IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException {
        return RsaUtil.decrypt(data, privateKey);
    }

    public static String reformatEncryptString(String data) {
        return data.replaceAll("\\s", "+");
    }
}
